<template>
	<view class="chatList">
		<view class="chatList_item" v-for="(item,index) in list" :key="index" @click="handleItem(item)">
			<view class="chatList_item_left">
				<image :src="item.user.avatar" mode=""></image>
			</view>
			<view class="chatList_item_right">
				<view class="chatList_item_right_top">
					<view class="chatList_item_right_top_nickName">
						{{item.user.nickname}}
					</view>
					<view class="chatList_item_right_top_lastTime">
						{{timestamp(item.updatetime)}}
					</view>
				</view>
				<view class="chatList_item_right_bottom">
					{{item.message}}
				</view>
			</view>
		</view>
		<view class="" style="margin-top: 20rpx;">
			<u-loadmore :status="status" is-dot/>
		</view>
	</view>
</template>

<script>
	import {
		getChatList
	} from '@/api/index.js'
	export default {
		data() {
			return {
				page: 1,
				list:[],
				status: 'loadmore',
			}
		},
		methods: {
			handleItem(val){
				uni.navigateTo({
					url:`/pages/views/privateChat/privateChat?chat_id=${val.id}&name=${val.user.nickname}`
				})
			},
			timestamp(timestamp) {
				var date = new Date(timestamp * 1000); // 转换为毫秒
				var year = date.getFullYear();
				var month = ('0' + (date.getMonth() + 1)).slice(-2);
				var day = ('0' + date.getDate()).slice(-2);
				var hours = ('0' + date.getHours()).slice(-2);
				var minutes = ('0' + date.getMinutes()).slice(-2);
				var seconds = ('0' + date.getSeconds()).slice(-2);
			
				return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
			},
			async getList() {
				this.status = 'loading'
				let data = {
					page: this.page,
					limit: 10
				}
				const res = await getChatList(data)
				this.list = [...this.list, ...res.data.data]
				if(res.data.data.length<10){
					this.status = 'nomore'
				}else{
					this.status = 'loadmore'
				}
			}
		},
		async onReachBottom(){
			if(this.status == 'nomore')return
			this.page++
			await this.getList()
		},
		async onShow() {
			this.page = 1
			this.list = []
			await this.getList()
		}
	}
</script>

<style scoped lang="scss">
	.chatList {
		min-height: 100vh;
		padding: 0 30rpx;

		.chatList_item {
			display: flex;
			justify-content: space-between;
			padding-top: 20rpx;
			.chatList_item_left{
				width: 100rpx;
				height: 100rpx;
				image{
					width: 100%;
					height: 100%;
					border-radius: 12rpx;
				}
			}
			.chatList_item_right{
				padding-bottom: 20rpx;
				height: 120rpx;
				border-bottom: 1rpx solid #ddd;
				width: 550rpx;
				display: flex;
				flex-direction: column;
				justify-content: center;
				.chatList_item_right_top{
					display: flex;
					align-items: center;
					justify-content: space-between;
					.chatList_item_right_top_nickName{
						font-size: 26rpx;
						font-weight: bold;
						line-height: 26rpx;
						color: #333;
						max-width: 350rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}
					.chatList_item_right_top_lastTime{
						font-size: 20rpx;
						line-height: 20rpx;
						color: #999;
					}
				}
				.chatList_item_right_bottom{
					font-size: 22rpx;
					color: #999;
					line-height: 22rpx;
					margin-top: 20rpx;
					max-width: 500rpx;
					text-overflow: ellipsis;
					overflow: hidden;
					white-space: nowrap;
				}
			}
		}
	}
</style>